package com.msun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.msun.dao.MemberDao;
import com.msun.dao.OrderDao;
import com.msun.dao.OrderListDao;
import com.msun.entity.PageResult;
import com.msun.pojo.Member;
import com.msun.pojo.Order;
import com.msun.service.OrderListService;
import com.msun.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderListService.class)
@Transactional
public class OrderListServiceImpl implements OrderListService {
    @Autowired
    private OrderListDao orderListDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;


    @Override
    public PageResult pageQuery(String[] selectDate, String selectStatus, String selectType, String queryString, Integer pageSize, Integer currentPage) {
        PageHelper.startPage(currentPage, pageSize);
        String startDate = null;
        String endDate = null;
        //截取日期字符串
        if (selectDate != null && selectDate.length > 0) {
            startDate = parseDateZone(selectDate[0]);
            endDate = parseDateZone(selectDate[1]);
        }
        Map<String, String> map = new HashMap<>();
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        Page<Map> page = orderListDao.selectByCondition(startDate, endDate, selectStatus, selectType, queryString);
        //处理日期格式
        if (page != null) {
            for (Map pageMap : page) {
                Date orderDate = (Date) pageMap.get("orderDate");
                String date = null;
                try {
                    date = DateUtils.parseDate2String(orderDate);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                pageMap.put("orderDate", date);
            }
        }
        return new PageResult(page.getTotal(), page.getResult());

    }
    //转换日期格式
    public String parseDateZone(String orderDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        String result = null;
        try {
            date = format.parse(orderDate);
            result = defaultFormat.format(date);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    //使用开关修改状态
    public void changeStatus(Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        String orderStatus = (String) map.get("orderStatus");
        Order order = orderListDao.findById(id);
        if (order != null) {
            order.setOrderStatus(orderStatus);
            map.put("orderStatus", orderStatus);
            orderListDao.changeStatus(map);
        }
    }

    //删除预约信息
    @Override
    public void delete(Integer id) {
        //判断当前信息是否关联其他表
        long count1 = orderListDao.deleteOrder1(id);
        long count2 = orderListDao.deleteOrder2(id);
        long count3 = orderListDao.deleteOrder3(id);

        if (count1 > 0 || count2 > 0 || count3 > 0){
            //当前id信息已关联其他表,不允许删除
            new RuntimeException("信息已关联其他表");
        }
        orderListDao.delete(id);
    }

    //预约添加
    @Override
    public void add(Map<String, Object> map) throws Exception {
        // 获取map中的参数
        List<Integer> setmealIds = (List<Integer>) map.get("setmealIds");
        if (setmealIds==null||setmealIds.size()<=0){
            // 没有选择套餐
            throw  new RuntimeException("套餐不能为空");
        }
        Map<String,Object> orderInfo = (Map<String, Object>) map.get("orderInfo");
        // 手机号
        String phoneNumber = (String) orderInfo.get("phoneNumber");
        // 预约日期并转换
        String orderDate1 = (String) orderInfo.get("orderDate");
        Date orderDate = DateUtils.parseString2Date(orderDate1, "yyyy-MM-dd");
        // 预约姓名
        String name = (String) orderInfo.get("name");
        System.out.println();
        //该手机号是否存在对应的会员
        Member member = memberDao.findByTelephone(phoneNumber);
        if (member == null) {
            //手机号不存在,自动注册并进行预约
            member = new Member();
            member.setName(name);
            member.setRegTime(new Date());
            member.setPhoneNumber(phoneNumber);
            memberDao.add(member);//自动完成会员注册
        }
        Order order = new Order();
        //手机号存在,直接添加订单
        order.setMemberId(member.getId());
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_TELEPHONE);
        order.setOrderDate(orderDate);
        System.out.println(member.getId());
        //  添加多个套餐信息
        for (Integer setmealId : setmealIds) {
            order.setSetmealId(setmealId);
            orderDao.add(order);
        }

    }

    @Override
    public void edit(Map<String, Object> map) {

        Integer MemberId = (Integer) map.get("member_id");

        String name = (String) map.get("name");

        String orderDate1 = (String) map.get("orderDate");//日期
        Date orderDate2 = null;
        String orderDate =null;
        try {
            orderDate2 = DateUtils.parseString2Date(orderDate1);
             orderDate = DateUtils.parseDate2String(orderDate2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String orderType = (String) map.get("orderType");//类型
        String orderStatus = (String) map.get("orderStatus");//预约状态

        Integer OrderId = (Integer) map.get("id");


        //根据手机号修改名字
        memberDao.editNameByMemberId(MemberId,name);

        orderDao.edit(OrderId,MemberId,orderDate,orderType,orderStatus);


        //orderListDao.edit(map);
    }

}

